function wav_info(filename)
% SENSORNET - Displays all info about the wav file.
% Usage:
%   wav_info('sample_sound.wav');
%


%% Wav header information
[m d] = wavfinfo(filename);

%% Read wav file
[y, Fs, nbits] = wavread(filename);
T = 1/Fs;           % Sample time (seconds)
L = length(y);      % Length of signal (number of samples)

duration = L*T;     % duration (seconds)


%% Calculate fft

NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2);

%% Main frequencies
% Main frequency is the position of the max value.
dsp = abs(Y(1:NFFT/2));

[mainpk_amplitude,mainpk_pos]=max(dsp);
mainpk_freq = round(f(mainpk_pos));
mph = 0.5 * mainpk_amplitude;

[pks,locs] = findpeaks(dsp,'minpeakheight',mph)

%TODO implement function: 
%[pks, locs] = find_main_peaks(signal, number)


%% Plot graphs

h = figure('Name', strcat('wav_info file:', filename));
hold on
subplot(2,1,1)
plot(y)
title(strcat(d, ' Sample Rate:',num2str(Fs), ' Nbits:', num2str(nbits), ' Duration: ',num2str(duration)) );
xlabel('samples')
ylabel('amplitude');

subplot(2,1,2)
% Plot single-sided amplitude spectrum.
plot(f,2*abs(Y(1:NFFT/2)))
title(strcat('Amplitude Spectrum, Main PEAK=', num2str(mainpk_freq) ));
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')

%%t = (0:L-1)*T;      % Time vector


%% Display information

disp (strcat('Filename:',filename));
disp (strcat('Info:',d));
disp (strcat('Sample Rate(Hz):',num2str(Fs)));
disp (strcat('Bits per sample:',num2str(nbits)));
disp (strcat('Duration (s):',num2str(duration)));
disp (strcat('Number of samples:',num2str(L)));

disp (strcat('FFT main frequency (Hz):',num2str(mainpk_freq)));
